﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Function
{
   public  class MessageEvent
    {

        #region 私有变量

        /// <summary>
        /// Cookie名称
        /// </summary>
        private static string m_CookieName = Method.SitePrefix + "_MessageBoxGuid";

        /// <summary>
        /// 消息显示页面
        /// </summary>
        private static string m_MessagePage = (ConfigurationManager.AppSettings["MessageBox"] == null) ? "ShowMessage.aspx" : ConfigurationManager.AppSettings["MessageBox"].ToString();

        #endregion

        #region 公有属性

        /// <summary>
        /// Cookie名称
        /// </summary>
        public static string CookieName
        {
            get
            {
                return string.Format("{0}-MessageValue", m_CookieName);
            }
        }

        #endregion

        #region 公有方法

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="msgbox"></param>
        public static void MessageBox(MessageBox_Page msgbox)
        {
            System.Random random = new Random(5 * (int)System.DateTime.Now.Ticks);
            string rStr = Convert.ToString(random.Next(100));
            rStr = string.Format("{0}?r={1}", m_MessagePage, rStr);
            System.Web.HttpContext.Current.Response.Cookies[CookieName].Value = Serializable_MessageBox(Serializable_MessageBox(msgbox));
            System.Web.HttpContext.Current.Response.Redirect(rStr);
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="msgbox"></param>
        public static void MessageBox(MessageBox_Page msgbox, string ToPage)
        {
            System.Web.HttpContext.Current.Response.Cookies[CookieName].Value = Serializable_MessageBox(Serializable_MessageBox(msgbox));
            System.Web.HttpContext.Current.Response.Redirect(ToPage);
        }

        /// <summary>
        /// 序列化MessageBox实例
        /// </summary>
        /// <param name="msgbox">MessageBox实例</param>
        /// <returns>字符数组</returns>
        public static byte[] Serializable_MessageBox(MessageBox_Page msgbox)
        {
            IFormatter formatter = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            byte[] result;
            formatter.Serialize(ms, msgbox);
            ms.Position = 0;
            result = new byte[ms.Length];
            ms.Read(result, 0, result.Length);
            ms.Close();
            return result;
        }

        /// <summary>
        /// 将字节数组转为ASCII字符
        /// </summary>
        /// <param name="MessageArray">字节数组</param>
        /// <returns></returns>
        public static string Serializable_MessageBox(byte[] MessageArray)
        {
            return Convert.ToBase64String(MessageArray);
        }

        /// <summary>
        /// 将字符按ASCII转为字节数组
        /// </summary>
        /// <param name="Messages"></param>
        /// <returns></returns>
        public static byte[] Deserialize_MessageBox(string Messages)
        {
            return Convert.FromBase64String(Messages);
        }

        /// <summary>
        /// 反序列化MessageBox类
        /// </summary>
        /// <param name="BytArray">字节内容</param>
        /// <returns></returns>
        public static MessageBox_Page Deserialize_MessageBox(byte[] BytArray)
        {
            IFormatter formatter = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            ms.Write(BytArray, 0, BytArray.Length);
            ms.Position = 0;
            MessageBox_Page msgbox = (MessageBox_Page)formatter.Deserialize(ms);
            return msgbox;
        }

        #endregion
    }

   /// <summary>
   /// 消息类
   /// </summary>
   [Serializable]
   public class MessageBox_Page
   {
       #region 私有变量

       /// <summary>
       /// 标题
       /// </summary>
       private string m_Title = "消息提示标题";

       /// <summary>
       /// 类型
       /// </summary>
       private MsgType m_Type = MsgType.Normal;

       /// <summary>
       /// 内容
       /// </summary>
       private IList<MessageContent> m_ContentItems = new List<MessageContent>();

       /// <summary>
       /// 链接
       /// </summary>
       private IList<MessageUrl> m_UrlItems = new List<MessageUrl>();

       #endregion

       #region 公有属性

       /// <summary>
       /// 标题
       /// </summary>
       public string Title
       {
           get { return this.m_Title; }
           set { this.m_Title = value; }
       }

       /// <summary>
       /// 类型
       /// </summary>
       public MsgType Type
       {
           get { return this.m_Type; }
           set { this.m_Type = value; }
       }

       /// <summary>
       /// 内容
       /// </summary>
       public IList<MessageContent> ContentItems
       {
           get { return this.m_ContentItems; }
           set { this.m_ContentItems = value; }
       }

       /// <summary>
       /// 链接
       /// </summary>
       public IList<MessageUrl> UrlItems
       {
           get { return this.m_UrlItems; }
           set { this.m_UrlItems = value; }
       }

       /// <summary>
       /// 内容大小
       /// </summary>
       public int GetContentCount
       {
           get
           {
               return this.m_ContentItems.Count;
           }
       }

       #endregion

       #region 公有方法

       /// <summary>
       /// 添加内容
       /// </summary>
       /// <param name="content">内容</param>
       public void AddContent(string content)
       {
           this.AddContent(content, MsgType.Error);
       }

       /// <summary>
       /// 添加内容
       /// </summary>
       /// <param name="content">内容</param>
       /// <param name="type">类型</param>
       public void AddContent(string content, MsgType type)
       {
           this.m_ContentItems.Add(new MessageContent(content, type));
       }

       /// <summary>
       /// 添加链接
       /// </summary>
       /// <param name="text">文字</param>
       /// <param name="url">地址</param>
       public void AddUrl(string text, string url)
       {
           this.AddUrl(text, text, url, UrlType.Href);
       }

       /// <summary>
       /// 添加链接
       /// </summary>
       /// <param name="text">文字</param>
       /// <param name="url">地址</param>
       /// <param name="type">类型</param>
       public void AddUrl(string text, string url, UrlType type)
       {
           this.AddUrl(text, text, url, type);
       }

       /// <summary>
       /// 添加链接
       /// </summary>
       /// <param name="text">文字</param>
       /// <param name="hint">提示</param>
       /// <param name="url">地址</param>
       /// <param name="type">类型</param>
       public void AddUrl(string text, string hint, string url, UrlType type)
       {
           this.m_UrlItems.Add(new MessageUrl(text, hint, url, type));
       }

       #endregion
   }

   #region 自定义枚举

   /// <summary>
   /// 消息类型
   /// </summary>
   public enum MsgType
   {
       /// <summary>
       /// 正常
       /// </summary>
       Normal,
       /// <summary>
       /// 成功
       /// </summary>
       Success,
       /// <summary>
       /// 失败
       /// </summary>
       Error,
       /// <summary>
       /// 警告
       /// </summary>
       Alarm
   }

   /// <summary>
   /// 链接类型
   /// </summary>
   public enum UrlType
   {
       /// <summary>
       /// 超级链接
       /// </summary>
       Href,
       /// <summary>
       /// Javascript脚本
       /// </summary>
       Javascript
   }

   #endregion

   #region 自定义实体

   /// <summary>
   /// 可序列化消息内容实体
   /// </summary>
   [Serializable]
   public class MessageContent
   {
       #region 私有变量

       /// <summary>
       /// 消息内容
       /// </summary>
       private string m_Content;

       /// <summary>
       /// 类型
       /// </summary>
       private MsgType m_Type;

       #endregion

       #region 构造函数

       /// <summary>
       /// 构造函数
       /// </summary>
       public MessageContent() { }

       /// <summary>
       /// 构造函数
       /// </summary>
       /// <param name="content">内容</param>
       /// <param name="type">类型</param>
       public MessageContent(string content, MsgType type)
       {
           this.m_Content = content;
           this.m_Type = type;
       }

       #endregion

       #region 公有属性

       /// <summary>
       /// 消息
       /// </summary>
       public string Content
       {
           get { return this.m_Content; }
           set { this.m_Content = value; }
       }

       /// <summary>
       /// 类型
       /// </summary>
       public MsgType Type
       {
           get { return this.m_Type; }
           set { this.m_Type = value; }
       }

       #endregion
   }

   /// <summary>
   /// 可序列化消息链接实体
   /// </summary>
   [Serializable]
   public class MessageUrl
   {
       #region 私有变量

       /// <summary>
       /// 链接文字
       /// </summary>
       private string m_Text;

       /// <summary>
       /// 链接提示
       /// </summary>
       private string m_Hint;

       /// <summary>
       /// 链接地址
       /// </summary>
       private string m_Url;

       /// <summary>
       /// 链接类型
       /// </summary>
       private UrlType m_Type = UrlType.Href;

       #endregion

       #region 构造函数

       /// <summary>
       /// 构造函数
       /// </summary>
       public MessageUrl() { }

       /// <summary>
       /// 构造函数
       /// </summary>
       /// <param name="text">链接文字</param>
       /// <param name="hint">链接提示</param>
       /// <param name="url">链接地址</param>
       /// <param name="type">链接类型</param>
       public MessageUrl(string text, string hint, string url, UrlType type)
       {
           m_Text = text;
           m_Hint = hint;
           m_Url = url;
           m_Type = type;
       }

       /// <summary>
       /// 构造函数
       /// </summary>
       /// <param name="text">链接文字</param>
       /// <param name="hint">链接提示</param>
       /// <param name="url">链接地址</param>
       public MessageUrl(string text, string hint, string url)
       {
           m_Text = text;
           m_Hint = hint;
           m_Url = url;
           m_Type = UrlType.Href;
       }

       #endregion

       #region 公有属性

       /// <summary>
       /// 链接文字
       /// </summary>
       public string Text
       {
           get { return this.m_Text; }
           set { this.m_Text = value; }
       }

       /// <summary>
       /// 链接提示
       /// </summary>
       public string Hint
       {
           get { return this.m_Hint; }
           set { this.m_Hint = value; }
       }

       /// <summary>
       /// 链接地址
       /// </summary>
       public string Url
       {
           get { return this.m_Url; }
           set { this.m_Url = value; }
       }

       /// <summary>
       /// 链接类型
       /// </summary>
       public UrlType Type
       {
           get { return this.m_Type; }
           set { this.m_Type = value; }
       }

       #endregion
   }

   #endregion
}
